// request请求
import store from '@/store/index';

const API_HOST = 'https://tr.ricking.cn/api';

export const request = function(url, data = {}, method = 'GET') {
  const token = store.state.token || null;
  uni.showLoading({title: '请求中...'});
  return new Promise((reslove, reject) => {
    uni.request({
      url: API_HOST + url,
      method: method,
      data: data,
      header: {
        'Content-type': 'application/json',
        token: token
      },
      success: res => {
        if (res.statusCode === 200) {
          reslove(res);
        } else if (res.data.code === 500) {
          uni.showToast({
						title: '服务器异常',
						icon: 'loading',
						duration: 1000
					});
        } else if (res.data.code === 401) {
          // 未登录
          // uni.showToast({
          //   title: '账号密码已失效',
          //   icon: 'none',
          //   duration: 1000
          // })
          // store.commit('loginout');
          reject(res);
        }
        uni.hideLoading();
      },
      fail: err => {
        reject(err);
        uni.hideLoading();
      }
    });
  });
}